package org.dao.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.bean.O2rankgame;
import org.dao.GameDao;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


public class GameDaoImpl extends HibernateDaoSupport implements GameDao {

	
	
	@SuppressWarnings("rawtypes")
	public List queryForPageGameForUid(final String hql,final int offset,final int length){   
		List list = getHibernateTemplate().executeFind(new HibernateCallback(){   
		public Object doInHibernate(Session session) throws HibernateException,SQLException{  
			Query query = session.createQuery(hql);
			query.setFirstResult(offset);   
			query.setMaxResults(length);  
			List list = query.list(); 
			
			return list;}   
		});
		return list;		
}
	
	
	public int getAllRowCount(String hql) {
		return getHibernateTemplate().find(hql).size();
	}

	@SuppressWarnings("rawtypes")
	@Override
	public O2rankgame SelectLastGameTimeByUid(int uid) {
		final String  hql = " from O2rankgame g where g.uid="+uid+" order by g.id desc";
		
		List list  = getHibernateTemplate().executeFind(new HibernateCallback(){   
			public Object doInHibernate(Session session) throws HibernateException,SQLException{  
				
				Query query = session.createQuery(hql);
				query.setFirstResult(0);   
				query.setMaxResults(1);  
				List list = query.list(); 
				return list;}   
			});
		O2rankgame o2rankgame =(O2rankgame)list.get(0);
				
				
		 
		return o2rankgame;

	}

	public O2rankgame SelectByid(int id) {
		final String  hql = " from O2rankgame g where g.id="+id;
		
		List list  = getHibernateTemplate().executeFind(new HibernateCallback(){   
			public Object doInHibernate(Session session) throws HibernateException,SQLException{  
				
				Query query = session.createQuery(hql);
				query.setFirstResult(0);   
				query.setMaxResults(1);  
				List list = query.list(); 
				return list;}   
			});
		O2rankgame o2rankgame =(O2rankgame)list.get(0);
				
				
		 
		return o2rankgame;

	}
	
	
	
	@SuppressWarnings({  "unchecked" })
	@Override
	public List<O2rankgame> SelectUserHistoryCapable(final String hql) {
		List<O2rankgame>  list = getHibernateTemplate().executeFind(new HibernateCallback(){   
			public Object doInHibernate(Session session) throws HibernateException,SQLException{  
				
				Query query = session.createSQLQuery(hql).addEntity(O2rankgame.class);
				query.setFirstResult(0);   
				query.setMaxResults(10);  
				List<O2rankgame> list = query.list(); 
				
				return list;}   
			});
			return list;		
	}
	
	@SuppressWarnings({  "unchecked" })
	@Override
	public List<O2rankgame> SelectTopCapable(final String hql) {
		List<O2rankgame>  list = getHibernateTemplate().executeFind(new HibernateCallback(){   
			public Object doInHibernate(Session session) throws HibernateException,SQLException{  
				
				Query query = session.createSQLQuery(hql).addEntity(O2rankgame.class);
				query.setFirstResult(0);   
				query.setMaxResults(10);  
				List<O2rankgame> list = query.list(); 
				
				return list;}   
			});
			return list;		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<O2rankgame> selectSongsTopByMid(final String hql) {
		
		List<O2rankgame>  list = getHibernateTemplate().executeFind(new HibernateCallback(){   
			public Object doInHibernate(Session session) throws HibernateException,SQLException{  
				
				Query query = session.createQuery(hql);
				query.setFirstResult(0);   
				query.setMaxResults(10);  
				List<O2rankgame> list = query.list(); 
				return list;}   
			});
			return list;	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<O2rankgame> selectSongsTopByName(final String hql) {
		List<O2rankgame>  list = getHibernateTemplate().executeFind(new HibernateCallback(){   
			public Object doInHibernate(Session session) throws HibernateException,SQLException{  
				
				Query query = session.createQuery(hql);
				query.setFirstResult(0);   
				query.setMaxResults(10); 
				List<O2rankgame> list = query.list(); 
				return list;}   
			});
			return list;	
	}

	
}

